<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\UserTable;
use App\Models\Family;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Widgets\Modal;
use Dcat\Admin\Http\Controllers\AdminController;

class FamilyController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Family(), function (Grid $grid) {
            $grid->column('name');
            $grid->column('house_num')->label();
            $grid->column('people_num')->display(function () {
                return $this->user()->get()->count();
            })->label('success');
            $grid->column('id', admin_trans('family.fields.family_people'))->display(function ($value) {
                return Modal::make()
                    ->lg()
                    ->title('家庭成员')
                    ->body(UserTable::make()->payload(['id' => $value])) // Modal 组件支持直接传递 渲染类实例
                    ->button('<i class="feather icon-align-right text-blue">'. trans('admin.view') .'</i>');
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('name')->width(3);
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Family(), function (Form $form) {
            $form->text('name')->required();
            $form->text('house_num')->type('number');
        });
    }
}
